package com.nhvu.compoundword.library;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Button;

import com.nhvu.compoundword.R;

public class Common {

	/**
	 * Set wrong background as player combine word wrong
	 * 
	 * @param btnCell
	 */
	public static void setBackgroundWrong(Button btnCell) {
		int kindPoint = Integer.parseInt(btnCell.getTag().toString());
		switch (kindPoint) {
		case MyGlobal.SinglePlayerValues.KIND_POINT_ONE:
			btnCell.setBackgroundResource(R.drawable.bg_wrong_10);
			break;
		case MyGlobal.SinglePlayerValues.KIND_POINT_TWO:
			btnCell.setBackgroundResource(R.drawable.bg_wrong_20);
			break;
		case MyGlobal.SinglePlayerValues.KIND_POINT_THREE:
			btnCell.setBackgroundResource(R.drawable.bg_wrong_30);
			break;
		default:
			btnCell.setBackgroundResource(R.drawable.bg_wrong_50);
			break;
		}
	}

	/**
	 * Set original background
	 * 
	 * @param btnCell
	 */
	public static void setBackgroundOriginal(Button btnCell) {
		int kindPoint = Integer.parseInt(btnCell.getTag().toString());
		switch (kindPoint) {
		case MyGlobal.SinglePlayerValues.KIND_POINT_ONE:
			btnCell.setBackgroundResource(R.drawable.bg_cell_10);
			break;
		case MyGlobal.SinglePlayerValues.KIND_POINT_TWO:
			btnCell.setBackgroundResource(R.drawable.bg_cell_20);
			break;
		case MyGlobal.SinglePlayerValues.KIND_POINT_THREE:
			btnCell.setBackgroundResource(R.drawable.bg_cell_30);
			break;
		default:
			btnCell.setBackgroundResource(R.drawable.bg_cell_50);
			break;
		}
	}

	/**
	 * Reset background
	 * 
	 * @param btnCell
	 */
	public static void resetBackground(Button btnCellStart, Button btnCellEnd) {
		if (btnCellStart != null)
			Common.setBackgroundOriginal(btnCellStart);
		if (btnCellEnd != null)
			Common.setBackgroundOriginal(btnCellEnd);
	}

	/**
	 * Set background as player click a cell
	 * 
	 * @param btnCell
	 */
	public static void setBackgroundClicked(Button btnCell) {
		int kindPoint = Integer.parseInt(btnCell.getTag().toString());
		switch (kindPoint) {
		case MyGlobal.SinglePlayerValues.KIND_POINT_ONE:
			btnCell.setBackgroundResource(R.drawable.bg_cellone_10);
			break;
		case MyGlobal.SinglePlayerValues.KIND_POINT_TWO:
			btnCell.setBackgroundResource(R.drawable.bg_cellone_20);
			break;
		case MyGlobal.SinglePlayerValues.KIND_POINT_THREE:
			btnCell.setBackgroundResource(R.drawable.bg_cellone_30);
			break;
		default:
			btnCell.setBackgroundResource(R.drawable.bg_cellone_50);
			break;
		}
	}

	/**
	 * Get total point from 2 cell
	 * 
	 * @param btnCellStart
	 * @param btnCellEnd
	 */
	public static int getPoint(Button btnCellStart, Button btnCellEnd) {
		int pointStart = Integer.parseInt(btnCellStart.getTag().toString());
		int pointEnd = Integer.parseInt(btnCellEnd.getTag().toString());
		return pointStart + pointEnd;
	}

	public static void closeApp(Activity activity) {
		activity.finish();
		System.exit(0);
	}

	public static Integer getCountInterAds(Activity activity) {
		SharedPreferences sharedpreference = activity.getApplicationContext()
				.getSharedPreferences(MyGlobal.Params.PLAYER_INFO, 0);
		Integer count = sharedpreference.getInt(MyGlobal.Params.COUNT_ADS, 0);
		return count;
	}

	public static void updateInterAds(Activity activity, int value) {
		SharedPreferences sharedpreference = activity.getApplicationContext()
				.getSharedPreferences(MyGlobal.Params.PLAYER_INFO, 0);
		Editor editor = sharedpreference.edit();
		editor.putInt(MyGlobal.Params.COUNT_ADS, value);
		editor.commit();
	}
}
